/*
Copyright 2008, Mark Arvidson

This file is part of EwaScorecard.

EwaScorecard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EwaScorecard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class EwaScorecard extends MIDlet implements CommandListener {

    public static String DEFAULTS_RECORD_NAME = "ScorecardDefaults";    // root display object
    private Display display;
    private ViewScoresForm viewScreen;
    private MeetData meetData;

    // constructor.
    public EwaScorecard() {
        display = Display.getDisplay(this);
        createMeet();
    }

    /**
     * Start the MIDlet by creating a list of
     * items and associating the
     * exit command with it.
     */
    public void startApp() throws MIDletStateChangeException {
        if (display.getCurrent() != null) {
            display.setCurrent(display.getCurrent());
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        viewScreen = null;
        meetData = null;
        notifyDestroyed();
    }

    public void promptToSave() {
        Alert saveAlert = new Alert("Data Changed", "Do you want to save?", null, AlertType.WARNING);
        saveAlert.addCommand(new Command("Save", Command.OK, 1));
        saveAlert.addCommand(new Command("Exit", Command.EXIT, 0));
        saveAlert.setCommandListener(new SaveCommandListener(this));
        display.setCurrent(saveAlert);
    }

    private class SaveCommandListener implements CommandListener {

        private EwaScorecard host;

        SaveCommandListener(EwaScorecard host) {
            this.host = host;
        }

        public void commandAction(Command c, Displayable d) {
            if (c.getCommandType() == Command.OK) {
                host.saveMeetData();
            } else {
                host.destroyApp(true);
            }
        }
    }

    // main menu
    public void mainMenu() {
        viewScores();
    }

    public void createMeet() {
        MeetForm meetForm = new MeetForm(this, meetData);
        display.setCurrent(meetForm);
    }

    public void setMeetData(MeetData meetData) {
        this.meetData = meetData;
        viewScreen = new ViewScoresForm(this, meetData);
        viewScores();
    }

    public void addGymnast() {
        AddGymnastForm gymnastForm = new AddGymnastForm(this, meetData);
        display.setCurrent(gymnastForm);
    }

    public void editGymnast(TeamData team, GymnastData gymnast) {
        AddGymnastForm gymnastForm = new AddGymnastForm(this, meetData,
                team, gymnast);
        display.setCurrent(gymnastForm);
    }

    public void showAbout() {
        AboutForm about = new AboutForm(this);
        display.setCurrent(about);
    }

    public void enterScore(int row, int col, int activeTeam) {
        EnterScoreForm scoreForm = new EnterScoreForm(this, meetData);
        scoreForm.edit(row, col, activeTeam);
        display.setCurrent(scoreForm);
    }

    public void saveAsDefaults() {
        try {
            meetData.storeRecord(DEFAULTS_RECORD_NAME);
        } catch (RecordStoreFullException e) {
            Alert alert = new Alert("Could not save", "Out of memory", null, AlertType.ERROR);
            display.setCurrent(alert);
        } catch (Exception ex) {
            Alert alert = new Alert("Could not save", null, null, AlertType.ERROR);
            display.setCurrent(alert);
        }
    }

    public void viewScores() {
        if (meetData == null) {
            createMeet();
        } else if (meetData.teams().size() == 0) {
            addGymnast();
        } else {
            display.setCurrent(viewScreen);
        }
    }

    public void saveMeetData() {
        SaveMeetDataForm saveForm = new SaveMeetDataForm(this, meetData);
        display.setCurrent(saveForm);
    }

    public void loadMeetData() {
        LoadMeetDataForm loadForm = new LoadMeetDataForm(this);
        display.setCurrent(loadForm);
    }

    public void commandAction(Command c, Displayable d) {
        switch (c.getCommandType()) {
            case Command.EXIT:
                if (meetData.hasChanged()) {
                    promptToSave();
                } else {
                    destroyApp(true);
                }
                break;
        }

    }
}

